//
//  NSString+Parsing.m
//  CocoaSMS
//
//  Created by Sooyong Wang on 09. 01. 31.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "NSString+Parsing.h"


@implementation NSString (Parsing)
- (NSString *)extractBetween:(NSString *)from and:(NSString *)to gates:(NSArray *)gates
{
	NSEnumerator *e;
	if ( gates == nil )
	{
		e = [[NSArray arrayWithObject:from] objectEnumerator];
	}
	else
	{
		NSMutableArray *ma = [NSMutableArray arrayWithArray:gates];
		[ma addObject:from];
		e = [ma objectEnumerator];
	}
	NSString *gate;
	NSString *me = [[self copy] autorelease];
	
	while ( gate = (NSString *)[e nextObject] )
	{
		NSRange range = [me rangeOfString:gate];
		if ( range.location == NSNotFound )
		{
			return nil;
		}
		me = [me substringFromIndex:range.location+[gate length]];
	}
	
	NSRange range = [me rangeOfString:to];
	if ( range.location == NSNotFound )
	{
		return nil;
	}
	
	return [me substringToIndex:range.location];
}

@end
